package com.fh.modules.system.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.constant.Constant;
import com.fh.common.system.log.service.SysLogMongoService;
import com.fh.common.util.ConvertUtils;
import com.fh.common.web.AbstractController;
import com.fh.modules.system.entity.SysLog;
import com.fh.modules.system.service.ISysLogService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author Qiu, Junda
 * @date 2019年4月3日
 * @description 系统日志表 前端控制器
 */
@RestController
@RequestMapping("/sys/log")
@Api("系统日志")
public class SysLogController extends AbstractController {

//	@Value("${log.indb.type:mysql}")
//	private String logIndbType;

    @Autowired
    private ISysLogService sysLogService;
//	@Autowired
//	private SysLogMongoService sysLogMongoService;

    /**
     * @param syslog
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     * @功能：查询日志记录
     */
    @ApiOperation("分页查询系统日志接口")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @AutoLog(value = "分页查询系统日志", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result<IPage<SysLog>> queryPageList(SysLog syslog,
                                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {

        Result<IPage<SysLog>> result = new Result<IPage<SysLog>>();
        String column = req.getParameter("column");
        String order = req.getParameter("order");

        String beginTime = req.getParameter("createTime_begin");
        String endTime = req.getParameter("createTime_end");

        String keyWord = req.getParameter("keyWord");

//		if (Constant.LOG_DB_MONGODB.equals(logIndbType)) {
//			result.setResult(
//					sysLogMongoService.queryPage(syslog, pageNo, pageSize, column, order, beginTime, endTime, keyWord));
//		} else if (Constant.LOG_DB_MYSQL.equals(logIndbType)) {
        QueryWrapper<SysLog> queryWrapper = new QueryWrapper<SysLog>(syslog);
        Page<SysLog> page = new Page<SysLog>(pageNo, pageSize);
        // 开始结束时间

        if (ConvertUtils.isNotEmpty(beginTime) && ConvertUtils.isNotEmpty(endTime)) {
            queryWrapper.ge(ConvertUtils.camelToUnderline("createTime"), beginTime);
            queryWrapper.le(ConvertUtils.camelToUnderline("createTime"), endTime);
        }
        // 排序逻辑 处理

        if (ConvertUtils.isNotEmpty(column) && ConvertUtils.isNotEmpty(order)) {
            if (Constant.ORDER_ASC.equals(order)) {
                queryWrapper.orderByAsc(ConvertUtils.camelToUnderline(column));
            } else {
                queryWrapper.orderByDesc(ConvertUtils.camelToUnderline(column));
            }
        }
        // 日志关键词
        if (ConvertUtils.isNotEmpty(keyWord)) {
            queryWrapper.like("log_content", keyWord);
        }
        IPage<SysLog> pageList = sysLogService.page(page, queryWrapper);
        result.setResult(pageList);
//		}

        result.setSuccess(true);
        return result;
    }

//	/**
//	 * @功能：删除单个日志记录
//	 * @param id
//	 * @return
//	 */
//	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
//	public Result<SysLog> delete(@RequestParam(name = "id", required = true) String id) {
//		Result<SysLog> result = new Result<SysLog>();
//		SysLog sysLog = sysLogService.getById(id);
//		if (sysLog == null) {
//			result.error500("未找到对应实体");
//		} else {
//			boolean ok = sysLogService.removeById(id);
//			if (ok) {
//				result.success("删除成功!");
//			}
//		}
//		return result;
//	}
//
//	/**
//	 * @功能：批量，全部清空日志记录
//	 * @param ids
//	 * @return
//	 */
//	@RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
//	public Result<SysRole> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
//		Result<SysRole> result = new Result<SysRole>();
//		if (ids == null || "".equals(ids.trim())) {
//			result.error500("参数不识别！");
//		} else {
//			if ("allclear".equals(ids)) {
//				this.sysLogService.removeAll();
//				result.success("清除成功!");
//			}
//			this.sysLogService.removeByIds(Arrays.asList(ids.split(",")));
//			result.success("删除成功!");
//		}
//		return result;
//	}

}
